# Maintainer: Markus Rathgeb <maggu2810@gmail.com>

_realname=restbed
_commit=6a9b52b3b8b4b37749f9b42b669de6b9568e8552
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.7.0.r1282.6a9b52b
pkgrel=2
pkgdesc="Corvusoft's Restbed framework brings asynchronous RESTful functionality to C++14 applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/Corvusoft/restbed"
license=('AGPL' 'CPL')
depends=("${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-asio"
             "git")
source=("${_realname}::git+https://github.com/Corvusoft/${_realname}#commit=$_commit"
        "0001-fix-CMakeLists.txt.patch"
        "0002-fix-dll-import-export-instructions.patch")
sha256sums=('SKIP'
            'a4f3bea758d595aad69a1e59417da02646509509ef78e7f92e87afe93e1226c5'
            '0f8f1f8564b0c82bd591bfea1de199e8be04cc7cfd57c01f049e319dcec72201')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "4.7.0.r%s.%s" "$(git rev-list --count "${_commit}")" "$(git rev-parse --short "${_commit}")"
}

prepare() {
  cd "${srcdir}/${_realname}"
  patch -Np1 -i "${srcdir}/0001-fix-CMakeLists.txt.patch"
  patch -Np1 -i "${srcdir}/0002-fix-dll-import-export-instructions.patch"
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      ${_extra_config[@]} \
      -DBUILD_TESTS=OFF \
      ../${_realname}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  # Shared Check
  cd "${srcdir}/build-${MSYSTEM}"
  PATH=$PWD/src:$PATH ${MINGW_PREFIX}/bin/ctest.exe ./ || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  # License
  install -Dm644 "${srcdir}/${_realname}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
